package org.adl.registry.types;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class Password extends RimParameter
{
    /**
     * Creates a new instance of Password
     */
    public Password(String iValue)
    {
        super("password", iValue);
        
        this.validate();
    }
    
    protected void validate()
    {
        String value = super.getParameterValue();
        
        if (value.trim().length() == 0)
        {
            super.getValidaionErrors().add("A password must be at least 8 characters in length.");
        }
        else
        {
            int upperCount = 0;
            int lowerCount = 0;
            int specialCount = 0;
            int numericCount = 0;
            
            char[] characters = value.toCharArray();
            
            for (int i = 0; i < characters.length; i++)
            {
                char character = characters[i];
                
                if (Character.isWhitespace(character))
                {
                    super.getValidaionErrors().add("A password cannot contain whitespace.");
                }
                else if(Character.isUpperCase(character))
                {
                    upperCount++;
                }
                else if(Character.isLowerCase(character))
                {
                    lowerCount++;
                }
                else if(Character.isDigit(character))
                {
                    numericCount++;
                }
                else
                {
                    char[] specialChars = this.getSpecialCharactersArray();
                    
                    for (int k = 0; k < specialChars.length; k++)
                    {
                        char special = specialChars[k];
                        
                        if (special == character)
                        {
                            specialCount++;
                            
                            break;
                        }
                    }
                }
            }
            
            if (upperCount < 2)
            {
                super.getValidaionErrors().add("A password must contain at least two upper case characters.");
            }
            
            if (lowerCount < 2)
            {
                super.getValidaionErrors().add("A password must contain at least two lower case characters.");
            }
            
            if (specialCount < 2)
            {
                super.getValidaionErrors().add("A password must contain at least two special character within the following set of special characters. [!@#$_]");
            }
        }
    }
    
    /**
     * 
     * @return 
     */
    public char[] getSpecialCharactersArray()
    {
        char[] characters = new char[] {'!','@','#','$','_'};
        
        return characters;
    }
}
